package com.lei.datastructure;

import java.util.Arrays;

/**
 * @Author LeiXiaoLei
 * @Date 2022/7/15 15:14
 * @Version 1.0
 */
@SuppressWarnings("all")
public class Array<E> {
    private E[] data;
    private int size;

    public Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    public Array(E[] arr) {
        data = (E[]) new Object[arr.length];
        for (int i = 0; i < arr.length; i++)
            data[i] = arr[i];
        size = data.length;
    }

    public Array() {
        this(10);
    }

    public E get(int index) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Get failed. Require index >=0 and <= size.");
        return data[index];
    }

    public E getLast() {
        return get(size - 1);
    }

    public E getFirst() {
        return get(0);
    }

    public void set(int index, E e) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Set failed. Require index >=0 and <= size.");
        data[index] = e;
    }

    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) return true;
        }
        return false;
    }

    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) return i;
        }
        return -1;
    }

    public E remove(int index) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("Remove failed. Require index >=0 and <= size.");
        E res = data[index];
        if (size - (index + 1) >= 0) System.arraycopy(data, index + 1, data, index + 1 - 1, size - (index + 1));
        --size;
        data[size] = null; // loitering objects != memory leak
        if (size == data.length / 4 && data.length > 20) {
            resize(data.length / 2);
        }
        return res;
    }

    public E removeFirst() {
        return remove(0);
    }

    public E removeLast() {
        return remove(size - 1);
    }

    public void removeElement(E e) {
        int index = find(e);
        if (index != -1) remove(index);
    }

    public int getSize() {
        return size;
    }

    public int getCapacity() {
        return data.length;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void addLast(E e) {
        add(size, e);
    }

    public void addFirst(E e) {
        add(0, e);
    }

    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        if (size >= 0) System.arraycopy(data, 0, newData, 0, size);
        data = newData;
    }

    public void add(int index, E e) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Add failed. Require index >=0 and <= size.");
        if (size == data.length) resize(2 * data.length);
        System.arraycopy(data, index, data, index + 1, size - index);
        data[index] = e;
        size++;
    }

    public void swap(int i, int j) {
        if (i < 0 || i >= size || j < 0 || j >= size) throw new IllegalArgumentException("Index is illegal");
        E t = data[i];
        data[i] = data[j];
        data[j] = t;
    }

    @Override
    public String toString() {
        return "Array{" +
                "size=" + size +
                ", capacity=" + data.length +
                ", data=" + Arrays.toString(data) +
                '}';
    }
}
